package com.changfang.gold_man.controller;

import com.changfang.gold_man.common.ResCode;
import com.changfang.gold_man.common.ResultVO;
import com.changfang.gold_man.entity.Task;
import com.changfang.gold_man.service.Imp.TaskServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/task")
@CrossOrigin(value = "*",maxAge = 1800,allowedHeaders = "*")

public class TaskController {
    @Autowired
    private TaskServiceImpl taskServiceImpl;
    //查询所有任务
    @GetMapping("/all")
    public List<Task> getTask(){
        return taskServiceImpl.findAll();
    }
    //查询所有待接受任务
    @GetMapping("/allEnable")
    public List<Task> getEnableTask(){
        return taskServiceImpl.findEnableTask();
    }
    //添加任务
    @PostMapping("/add")
    public ResultVO addTask(@RequestBody Map<String, String> task) {
        String flag = taskServiceImpl.addTask(task);
        if (flag.equals("1")){
            return new ResultVO(ResCode.SUCCESS, "添加任务成功");
        }else {
            return new ResultVO(ResCode.ERROR, flag);
        }
    }
    //    根据任务id查询任务
    @GetMapping("/id")
    public ResultVO getId(@RequestParam String task_id) {
        try {
            return new ResultVO(ResCode.SUCCESS, taskServiceImpl.findId(task_id));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "查询失败");
        }
    }
    //    根据名称查任务
    @GetMapping("/name")
    public ResultVO getName(@RequestParam String task_name) {
        try {
            return new ResultVO(ResCode.SUCCESS, taskServiceImpl.findName(task_name));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "查询失败");
        }
    }
    //接受任务
    @GetMapping("/receive")
    public ResultVO receive(@RequestParam String task_id,@RequestParam String user_id) {
        taskServiceImpl.receive(task_id,user_id);
        return new ResultVO(ResCode.SUCCESS, "接受成功");
    }
    //完成任务
    @GetMapping("/complete")
    public ResultVO complete(@RequestParam String task_id){
        if (taskServiceImpl.complete(Integer.parseInt(task_id)).equals("success")){
            return new ResultVO(ResCode.SUCCESS, "success");
        }else {
            return new ResultVO(ResCode.ERROR, "error");
        }
    }
    //查询我接受的任务
    @GetMapping("/myReceive")
    public ResultVO myReceive(@RequestParam String receiver_id){
        try {
            return new ResultVO(ResCode.SUCCESS, taskServiceImpl.findMyReceive(receiver_id));
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, e);
        }

    }
    //删除任务
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam String task_id){
        try {
            taskServiceImpl.delete(task_id);
            return new ResultVO(ResCode.SUCCESS, "删除任务成功");
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, "删除任务失败");
        }
    }
    //创建者ID查任务
    @GetMapping("/findCreater")
    public ResultVO findCreater(@RequestParam String creater_id){
        try {
            return new ResultVO(ResCode.SUCCESS, taskServiceImpl.findMyCreater(creater_id));
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, "查询发布任务失败");
        }
    }
    //关键字查任务
    @GetMapping("/findKeyStr")
    public ResultVO findKeyStr(@RequestParam String keyStr){
        try {
            return new ResultVO(ResCode.SUCCESS, taskServiceImpl.findKeyStr(keyStr));
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, "查询关键字任务失败");
        }
    }

}
